// <copyright file="ActivateBasicWebPartsFeature.EventReceiver.cs" company="Blue Rooster">
//     Blue Rooster. All rights reserved.
// </copyright>
// <author>Adam Toth</author>
namespace ActivateBasicWebPartsFeature.Features.ActivateBasicWebPartsFeature
{
    using System;
    using System.Runtime.InteropServices;
    using System.Security.Permissions;
    using Microsoft.SharePoint;

    /// <summary>
    /// This class handles events raised during feature activation, deactivation, installation, uninstallation, and upgrade.
    /// </summary>
    /// <remarks>
    /// The GUID attached to this class may be used during packaging and should not be modified.
    /// </remarks>
    [Guid("d8286411-ae8e-4b7c-bd07-c5293313e3cb")]
    public class ActivateBasicWebPartsFeatureEventReceiver : SPFeatureReceiver
    {
        /// <summary>
        /// Activates the basic web part feature.
        /// </summary>
        /// <param name="properties">A <see cref="SPFeatureReceiverProperties"/> object.</param>
        public override void FeatureActivated(SPFeatureReceiverProperties properties)
        {
            if (properties == null)
            {
                throw new ArgumentNullException("properties");
            }

            SPSite site = properties.UserCodeSite;

            if (site == null)
            {
                throw new InvalidOperationException("Could not find an SPSite.");
            }

            if (site.Features[new Guid("00bfea71-1c5e-4a24-b310-ba51c3eb7a57")] == null)
            {
                site.Features.Add(new Guid("00bfea71-1c5e-4a24-b310-ba51c3eb7a57"));
            }
        }

        /// <summary>
        /// Deactivates the Basic Web Parts Feature
        /// </summary>
        /// <param name="properties">A <see cref="SPFeatureReceiverProperties"/> object.</param>
        public override void FeatureDeactivating(SPFeatureReceiverProperties properties)
        {
            if (properties == null)
            {
                throw new ArgumentNullException("properties");
            }

            SPSite site = properties.UserCodeSite;

            if (site == null)
            {
                throw new InvalidOperationException("Could not find an SPSite.");
            }

            if (site.Features[new Guid("00bfea71-1c5e-4a24-b310-ba51c3eb7a57")] != null)
            {
                site.Features.Remove(new Guid("00bfea71-1c5e-4a24-b310-ba51c3eb7a57"));
            }
        }

        //// Uncomment the method below to handle the event raised after a feature has been installed.
        ////public override void FeatureInstalled(SPFeatureReceiverProperties properties)
        ////{
        ////}
        //// Uncomment the method below to handle the event raised before a feature is uninstalled.
        ////public override void FeatureUninstalling(SPFeatureReceiverProperties properties)
        ////{
        ////}
        //// Uncomment the method below to handle the event raised when a feature is upgrading.
        ////public override void FeatureUpgrading(SPFeatureReceiverProperties properties, string upgradeActionName, System.Collections.Generic.IDictionary<string, string> parameters)
        ////{
        ////}
    }
}